package com.healthcarex.hip.saas.web.config;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author ChenPan
 * @ClassName: Swagger2
 * description: Swagger2 API文档
 * @date 2017/2/23 15:18
 */

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Autowired
    private SwaggerProperties swaggerPro;

    @Bean
    public Docket createRestApi() {
        //apiInfo()用来创建该Api的基本信息
        //select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现
        //Swagger会扫描该包下所有Controller定义的API，并产生文档内容（除了被@ApiIgnore指定的请求）。
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerPro.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(Lists.newArrayList(apiKey()));
    }

    private ApiInfo apiInfo() {

        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder()
                .title(swaggerPro.getDocTitle())
                .description(swaggerPro.getDocDesc())
                .termsOfServiceUrl(swaggerPro.getServiceUrl());
        swaggerPro.getContacts().forEach(contact -> apiInfoBuilder.contact(
                new Contact(contact.getName(), contact.getUrl(), contact.getEmail())
        ));

        return apiInfoBuilder.version(swaggerPro.getVersion()).build();
    }

    @Bean
    SecurityScheme apiKey() {
        return new ApiKey("apiKey", "Authorization", "header");
    }

}
